package com.wanpinghui.supplier.user.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.wphlibrary.model.user.bean.RechargePlansBean;
import com.wanpinghui.wphlibrary.widgets.ThrottleButton;
import com.yunshl.yunshllibrary.recyclerview.BaseRecyclerViewAdapter;
import com.yunshl.yunshllibrary.utils.NumberUtil;
import com.yunshl.yunshllibrary.utils.TextUtil;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/12/6
 ***/

public class PlansListAdapter extends BaseRecyclerViewAdapter<RechargePlansBean, PlansListAdapter.PlansHolder> {

    public PlansListAdapter(Context context) {
        super(context);
    }

    @Override
    public PlansHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_minepackage, null);
        return new PlansHolder(view);
    }

    public RechargePlansBean getData(int position) {
        if (position < getItemCount()) {
            return datas.get(position);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final PlansHolder holder, final int position) {
        RechargePlansBean bean = datas.get(position);
        if (TextUtil.isNotEmpty(bean.getTag())) {
            holder.textViewBidPoint.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryYellow));
            holder.textViewSmsPoint.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryYellow));
            holder.textViewPrice.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryYellow));
            holder.textViewPromotion.setVisibility(View.VISIBLE);
            holder.linearLayoutItem.setBackgroundResource(R.drawable.shape_bg_plans_havetag);
            holder.textViewPromotion.setText(bean.getTag());
            holder.buttonPay.setBackgroundResource(R.drawable.select_common_btn_yellow_radius);
            holder.textViewOldPrice.setVisibility(View.GONE);
        } else {
            holder.textViewBidPoint.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryBlue));
            holder.textViewSmsPoint.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryBlue));
            holder.textViewPrice.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryBlue));
            holder.textViewPromotion.setVisibility(View.GONE);
            holder.textViewOldPrice.setVisibility(View.VISIBLE);
            holder.linearLayoutItem.setBackgroundResource(R.drawable.shape_bg_plans_notag);
            holder.buttonPay.setBackgroundResource(R.drawable.selector_common_btn_blue_radius2);
            SpannableString string = new SpannableString("￥" + NumberUtil.double2StringAdaptive(bean.getOri_price()));
            string.setSpan(new StrikethroughSpan(), 0, string.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            holder.textViewOldPrice.setText(string);
        }
        holder.textViewBidPoint.setText(bean.getBid_point() + "个");
        //holder.textViewSmsPoint.setText(bean.getSms_point()+"条");
        holder.textViewPrice.setText(NumberUtil.double2StringAdaptive(bean.getPrice()) + "元/月");
        holder.buttonPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(holder.buttonPay, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    class PlansHolder extends RecyclerView.ViewHolder {
        TextView textViewBidPoint;
        TextView textViewSmsPoint;
        TextView textViewPromotion;
        TextView textViewOldPrice;
        TextView textViewPrice;
        ThrottleButton buttonPay;
        LinearLayout linearLayoutItem;

        PlansHolder(View itemView) {
            super(itemView);
            textViewBidPoint = (TextView) itemView.findViewById(R.id.tv_bid_point);
            textViewSmsPoint = (TextView) itemView.findViewById(R.id.tv_sms_point);
            textViewPromotion = (TextView) itemView.findViewById(R.id.tv_promotion);
            textViewOldPrice = (TextView) itemView.findViewById(R.id.tv_old_price);
            textViewPrice = (TextView) itemView.findViewById(R.id.tv_price);
            buttonPay = (ThrottleButton) itemView.findViewById(R.id.btn_pay);
            linearLayoutItem = (LinearLayout) itemView.findViewById(R.id.ll_item);
            itemView.setLayoutParams(new GridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
    }
}
